package com.iyundao.io.mapper;

import com.iyundao.io.model.ActivityModel;
import com.iyundao.io.tools.ActitvityShow;
import com.iyundao.io.tools.ActivityModelVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ActivityModelMapper {
    @Insert({
        "insert into activitymodel (id, activitytitle, ",
        "activitycontent, activitystarttime, ",
        "activityendtime, activitysignstarttime, ",
        "activitysignendtime, activitylongitude, ",
        "activitylatitude, activitysitename, ",
        "activitycreatetime, activitynumber, ",
        "activityjoinnumber, activityintegral, ",
        "activityimg, userid, ",
        "activityflag, activityid, ",
        "activitydigest, organizationid)",
        "values (#{id,jdbcType=INTEGER}, #{activitytitle,jdbcType=VARCHAR}, ",
        "#{activitycontent,jdbcType=VARCHAR}, #{activitystarttime,jdbcType=TIMESTAMP}, ",
        "#{activityendtime,jdbcType=TIMESTAMP}, #{activitysignstarttime,jdbcType=TIMESTAMP}, ",
        "#{activitysignendtime,jdbcType=TIMESTAMP}, #{activitylongitude,jdbcType=DOUBLE}, ",
        "#{activitylatitude,jdbcType=DOUBLE}, #{activitysitename,jdbcType=VARCHAR}, ",
        "#{activitycreatetime,jdbcType=TIMESTAMP}, #{activitynumber,jdbcType=INTEGER}, ",
        "#{activityjoinnumber,jdbcType=INTEGER}, #{activityintegral,jdbcType=INTEGER}, ",
        "#{activityimg,jdbcType=VARCHAR}, #{userid,jdbcType=VARCHAR}, ",
        "#{activityflag,jdbcType=INTEGER}, #{activityid,jdbcType=INTEGER}, ",
        "#{activitydigest,jdbcType=VARCHAR}, #{organizationid,jdbcType=VARCHAR})"
    })
    int insert(ActivityModel record);

    @InsertProvider(type=ActivityModelSqlProvider.class, method="insertSelective")
    int insertSelective(ActivityModel record);

    @Select("select * from activitymodel where id=#{id,jdbcType=INTEGER}")
    ActivityModel selectById(Integer id);

    @Select("select * from activitymodel where organizationid=#{organizationid,jdbcType=VARCHAR}")
    List<ActivityModel> selectAllByOrganizationId(String organizationid);

    @Select("select * from activitymodel where organizationid=#{organizationid,jdbcType=VARCHAR} and activityflag=#{activityflag,jdbcType=INTEGER}")
    List<ActivityModel> findByOrganizationIdAndFlag(String organizationid, Integer activityflag);

    @Update("update activitymodel set activityflag=#{activityflag,jdbcType=INTEGER} where id=#{id,jdbcType=INTEGER}")
    int updateFlag(Integer activityflag, Integer id);

    @Select("select id from activitymodel order by id desc LIMIT 1")
    int selectLastId();

    @Update("update activitymodel set activityjoinnumber=#{activityjoinnumber,jdbcType=INTEGER} where id=#{id,jdbcType=INTEGER}")
    int updateJoinNum(Integer activityjoinnumber, Integer id);

    @Select("select * from activitymodel where activitytitle like #{activitytitle,jdbcType=VARCHAR}")
    List<ActivityModel> selectByLike(String activitytitle);

    @Select("select * from activitymodel where id=#{id,jdbcType=INTEGER}")
    ActitvityShow selectAllById(Integer id);

    @Select("select a.id, a.activitytitle,a.activitycontent,DATE_FORMAT(a.activitystarttime,'%Y-%m-%d %H:%i:%s') activitystarttime,DATE_FORMAT(a.activityendtime,'%Y-%m-%d %H:%i:%s') activityendtime, " +
            "DATE_FORMAT(a.activitysignstarttime,'%Y-%m-%d %H:%i:%s') activitysignstarttime, DATE_FORMAT(a.activitysignendtime,'%Y-%m-%d %H:%i:%s') activitysignendtime, a.activitylongitude, a.activitylatitude, " +
            "a.activitysitename, DATE_FORMAT(a.activitycreatetime,'%Y-%m-%d %H:%i:%s') activitycreatetime, a.activitynumber, a.activityjoinnumber, a.activityintegral, " +
            "a.activityimg, a.userid, a.activityflag, a.activityid, a.activitydigest, a.organizationid,ac.categoryname,ac.categoryintegral" +
            " from activitymodel a " +
            "INNER JOIN activitycategory ac " +
            " on a.activityid= ac.id " +
            "where a.id=#{id,jdbcType=INTEGER}")
    ActivityModelVo selectShowById(Integer id);

    @Select("select a.id, a.activitytitle,a.activitycontent,DATE_FORMAT(a.activitystarttime,'%Y-%m-%d %H:%i:%s') activitystarttime,DATE_FORMAT(a.activityendtime,'%Y-%m-%d %H:%i:%s') activityendtime, " +
            "DATE_FORMAT(a.activitysignstarttime,'%Y-%m-%d %H:%i:%s') activitysignstarttime, DATE_FORMAT(a.activitysignendtime,'%Y-%m-%d %H:%i:%s') activitysignendtime, a.activitylongitude, a.activitylatitude, " +
            "a.activitysitename, DATE_FORMAT(a.activitycreatetime,'%Y-%m-%d %H:%i:%s') activitycreatetime, a.activitynumber, a.activityjoinnumber, a.activityintegral, " +
            "a.activityimg, a.userid, a.activityflag, a.activityid, a.activitydigest, a.organizationid,ac.categoryname,ac.categoryintegral" +
            " from activitymodel a " +
            "INNER JOIN activitycategory ac " +
            " on a.activityid= ac.id " +
            "where a.activityflag=1")
    List<ActivityModelVo> selectAll();

    @Select("select a.id, a.activitytitle,a.activitycontent,DATE_FORMAT(a.activitystarttime,'%Y-%m-%d %H:%i:%s') activitystarttime,DATE_FORMAT(a.activityendtime,'%Y-%m-%d %H:%i:%s') activityendtime, " +
            "DATE_FORMAT(a.activitysignstarttime,'%Y-%m-%d %H:%i:%s') activitysignstarttime, DATE_FORMAT(a.activitysignendtime,'%Y-%m-%d %H:%i:%s') activitysignendtime, a.activitylongitude, a.activitylatitude, " +
            "a.activitysitename, DATE_FORMAT(a.activitycreatetime,'%Y-%m-%d %H:%i:%s') activitycreatetime, a.activitynumber, a.activityjoinnumber, a.activityintegral, " +
            "a.activityimg, a.userid, a.activityflag, a.activityid, a.activitydigest, a.organizationid,ac.categoryname,ac.categoryintegral" +
            " from activitymodel a " +
            "INNER JOIN activitycategory ac " +
            " on a.activityid= ac.id " +
            "where a.activityflag=1 and a.organizationid=#{organizationid,jdbcType=VARCHAR}")
    List<ActivityModelVo> selectAllBy(String organizationid);

    @Update("update activitymodel set activitytitle=#{activitytitle,jdbcType=VARCHAR}, activitycontent=#{activitycontent,jdbcType=VARCHAR}, activitystarttime=#{activitystarttime,jdbcType=TIMESTAMP}, " +
            "activityendtime=#{activityendtime,jdbcType=TIMESTAMP}, activitysignstarttime=#{activitysignstarttime,jdbcType=TIMESTAMP}, activitysignendtime=#{activitysignendtime,jdbcType=TIMESTAMP}, activitylongitude=#{activitylongitude,jdbcType=DOUBLE}, " +
            "activitylatitude=#{activitylatitude,jdbcType=DOUBLE}, activitysitename=#{activitysitename,jdbcType=VARCHAR}, activitycreatetime=#{activitycreatetime,jdbcType=TIMESTAMP}, activitynumber= #{activitynumber,jdbcType=INTEGER}, " +
            "activityjoinnumber=#{activityjoinnumber,jdbcType=INTEGER}, activityintegral=#{activityintegral,jdbcType=INTEGER}, activityimg=#{activityimg,jdbcType=VARCHAR}, userid=#{userid,jdbcType=INTEGER}, " +
            "activityflag=1, activityid=#{activityid,jdbcType=INTEGER},activitydigest=#{activitydigest,jdbcType=VARCHAR}, organizationid=#{organizationid,jdbcType=VARCHAR} " +
            "where id = #{id,jdbcType=INTEGER}")
    int check(ActivityModel activityModel);
}